from fastapi import APIRouter, Depends
from starlette.responses import Response

from src.auth.jwt import parse_jwt_user_data
from src.auth.schemas import JWTData
from src.models import CustomModel

router = APIRouter()


# get请求代码样例
@router.get("/test", include_in_schema=False)
async def test(
        jwt_data: JWTData = Depends(parse_jwt_user_data),
) -> str:
    return "hello world!"


# 定义api 请求返回的Response Model
class ApiReponse(CustomModel):
    code: str
    message: str
    data: dict


class TestRequest(CustomModel):
    name: str


# 定义post请求样例, 并且返回ApiReponse,请求参数为TestRequest

@router.post("/test2", response_model=ApiReponse)
async def test2(data: TestRequest) -> ApiReponse:
    return ApiReponse(
        code="200",
        message="success",
        data={"name": data.name}
    )


# sse定义streaming请求样例
@router.get("/stream")
async def stream():
    def event_stream():
        while True:
            yield "data: Hello, world!\n\n"
    return Response(event_stream(), media_type="text/event-stream")